package cn.yudao.ymcc.feignconfig;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


@Component
public class TokenFeignInterceptor implements RequestInterceptor {

    
    @Override
    public void apply(RequestTemplate requestTemplate) {

        //拿到请求头 -- 需要强行转换为ServletRequestAttributes类型
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        //通过key值拿到token
        String authorization = request.getHeader("Authorization");
        if (authorization != null){
            // 把得到的token放到发送请求的请求体中
            requestTemplate.header("Authorization",authorization);
        }

    }

}